package com.item 
{
	import com.greensock.TweenLite;
	import com.sound.SoundCtrl;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	/**
	 * ...
	 * @author wen
	 */
	public class Bells extends ItemBase 
	{
		public const R:Number = 110;
		
		public const Nums:int = 16;
		
		private var _stars:Array = null;
		
		public function Bells(mc:MovieClip) 
		{
			super(mc);
		}
		
		/**
		 * 初始化
		 */
		override public function init():void 
		{
			_stars = new Array();
			for (var i:int = 0; i < Nums; i++ )
			{
				var o:starMc = new starMc();
				_stars.push(o);
				view.addChild(o);
				o.scaleX = o.scaleY = 1;
			}
			
			clear();
		}
		
		public function setScale(scale:Number):void
		{
			var temp:Number = Math.max((1.6 - scale / 10), 0.5);
			
			view.a.scaleX = temp;
			view.a.scaleY = temp;
			view.bell.scaleX = temp;
			view.bell.scaleY = temp;
		}
		
		/**
		 * 显示星星效果
		 * @param	scale
		 */
		public function showStars():void
		{
			resetStars();
			for (var i:int = 0; i < Nums; i++ )
			{
				_stars[i].visible = true;
				_stars[i].alpha = 0;
				_stars[i].rotation = 0;
				
				var tmpX:Number = _stars[i].x + R * Math.cos(i*2 * Math.PI / Nums);
				var tmpY:Number = _stars[i].y + R * Math.sin(i*2 * Math.PI / Nums);
				
				TweenLite.to(_stars[i], 0.4, {x:tmpX, y:tmpY, alpha:1  } );
				
			}
		}
		
		//private function onStarsBack(target:MovieClip):void 
		//{
			//TweenLite.to(target, 0.6, { y:target.y+40, alpha:0} );
		//}
		
		private function resetStars():void 
		{
			for (var i:int = 0; i < _stars.length; i++ )
			{
				(_stars[i] as MovieClip).visible = false;
				(_stars[i] as MovieClip).x = view.bell.x;
				(_stars[i] as MovieClip).y = view.bell.y;
			}
		}
		
		
		/**
		 * 被踩中是的动画
		 */
		public function stepOn(num:int=0):void
		{
			SoundCtrl.instance.soundPlay("bell");
			
			view.label.visible = true;
			view.a.visible = true;
			view.bell.visible = false;
			
			view.a.gotoAndPlay(1);
			
			view.label.text = num;
			
			showStars(); 
			
			setTimeout(hide, 500);
		}
		
		/**
		 * 隐藏
		 */
		public function hide():void
		{
			TweenLite.to(view.label, 0.5, { y:view.label.y + 40, alpha:0 ,onComplete:clearView} );
			
			for (var i:int = 0; i < Nums; i++ )
			{
				TweenLite.to(_stars[i], 0.5, { y:_stars[i].y+40, alpha:0} );
			}
		}
		
		public function clearView():void 
		{
			while (view.numChildren > 1)
			{
				view.removeChildAt(0);
			}
			view.parent.removeChild(view);
		}
		
		/**
		 * 清空
		 */
		override public function clear():void 
		{
			view.label.text = "";
			
			view.label.visible = false;
			view.a.visible = false;
			view.bell.visible = true;
			
			resetStars();
		}
	}

}